﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp1.Tools;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private List<ScheduledTask> tasks = new List<ScheduledTask>();
        private BindingSource bindingSource = new BindingSource();
        public Form1()
        {
            InitializeComponent();
            SetupDataGridView();
        }
        private void SetupDataGridView()
        {
            dgvTasks.AutoGenerateColumns = false;
            dgvTasks.DataSource = bindingSource;

            // 添加列
            dgvTasks.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "Name",
                HeaderText = "任务名称",
                Name = "colName"
            });

            dgvTasks.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "Interval",
                HeaderText = "间隔(秒)",
                Name = "colInterval"
            });

            dgvTasks.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "IsActive",
                HeaderText = "状态",
                Name = "colStatus",
                ValueType = typeof(string)
            });

            dgvTasks.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "NextRunTime",
                HeaderText = "下次运行时间",
                Name = "colNextRun"
            });

            dgvTasks.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "LastRunTime",
                HeaderText = "上次运行时间",
                Name = "colLastRun"
            });

            dgvTasks.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "RunCount",
                HeaderText = "运行次数",
                Name = "colRunCount"
            });
        }
        private void RefreshDataGridView()
        {
            bindingSource.DataSource = tasks.Select(t => new
            {
                t.Name,
                t.Interval,
                Status = t.IsActive ? "运行中" : "已暂停",
                NextRunTime = t.IsActive ? t.NextRunTime.ToString("yyyy-MM-dd HH:mm:ss") : "N/A",
                LastRunTime = t.LastRunTime == DateTime.MinValue ? "N/A" : t.LastRunTime.ToString("yyyy-MM-dd HH:mm:ss"),
                t.RunCount
            }).ToList();

            dgvTasks.Refresh();
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            //groupBoxAddTask.Visible = !groupBoxAddTask.Visible;
        }

        private void btnConfirmAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtName.Text))
            {
                MessageBox.Show("请输入任务名称");
                return;
            }

            if (numInterval.Value <= 0)
            {
                MessageBox.Show("间隔时间必须大于0");
                return;
            }

            string taskName = txtName.Text;
            int interval = (int)numInterval.Value;

            // 创建任务动作
            Action taskAction = () =>
            {
                // 这里写任务的具体逻辑
                //MessageBox.Show($"任务[{taskName}]执行中...");
                ConcurrentLogger.Log(LogLevel.Info, $"任务[{taskName}]执行中...", "Startup");
            };

            // 创建并添加任务
            var task = new ScheduledTask(taskName, interval, taskAction);
            tasks.Add(task);

            // 重置UI
            txtName.Text = "";
            numInterval.Value = 1;
            //groupBoxAddTask.Visible = false;

            RefreshDataGridView();
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            if (dgvTasks.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要暂停的任务");
                return;
            }

            int selectedIndex = dgvTasks.SelectedRows[0].Index;
            tasks[selectedIndex].Pause();
            RefreshDataGridView();
        }

        private void btnResume_Click(object sender, EventArgs e)
        {
            if (dgvTasks.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要恢复的任务");
                return;
            }

            int selectedIndex = dgvTasks.SelectedRows[0].Index;
            tasks[selectedIndex].Resume();
            RefreshDataGridView();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvTasks.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要删除的任务");
                return;
            }

            int selectedIndex = dgvTasks.SelectedRows[0].Index;
            tasks[selectedIndex].Stop();
            tasks.RemoveAt(selectedIndex);
            RefreshDataGridView();
        }

        // 窗体关闭时清理所有定时器
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (var task in tasks)
            {
                task.Stop();
            }
        }

        // 定时刷新UI显示
        private void timerUI_Tick(object sender, EventArgs e)
        {
            RefreshDataGridView();
        }
    }
}
